var MY_SEWAGE_TEST_REPORT_COM = (function() {

	function getThead(){
		var e='<tr>\
	          <th width="15%" class="text-center">机构名/科室</th>\
	          <th width="5%" class="text-center">年月</th>\
	          <th width="5%" class="text-center">状态</th>\
	          <th width="8%" class="text-center">状态说明</th>\
	          <th width="8%" class="text-center">报告单</th>\
	          <th width="6%" class="text-center">检测日期</th>\
	          <th width="6%" class="text-center">申述状态</th>\
	          <th width="8%" class="text-center">申述内容</th>\
	          <th width="8%" class="text-center">照片</th>\
	          <th width="8%" class="text-center">处理意见</th>\
	          <th width="5%" class="text-center">处理人</th>\
	          <th width="6%" class="text-center">操作</th>\
	        </tr>';	
		return e;
	}
	
	function loadItem(params,$thead,$tbody){
		$thead.empty();
		$thead.append(getThead());
		$tbody.empty();

		WeKnow.loading();

		$.ajax({ 
	        type: "POST", 
	        url: "invs_sewage_test_report.do/load", 
	        dataType: "json",
	        contentType:"application/json",
	        data: JSON.stringify(params),
	        async: true,//异步调用
	        success: function(data) {
	        	
	        	WeKnow.closeLoading();
	        	
				if (!WeKnow.filterAjaxResult(data)) return false; // session过期check
	        	
	        	if (data.errcode == 0) {
	        		if ( 'datalist' in data && data.datalist.length == 0 ){
	        			WeKnow.errorMsg('没有查询到医疗机构数据');
                        $tbody.empty();
	        			 return;
	        		}
	        		var e = '';
	        		$(data.datalist).each(function( idx,_a){
	        			e+='<tr class="pointer" ';
		        				e+= ' data-institution_id="' + _a.uploadEntity.institutionId + '"';
		        				e+= ' data-institution_name="' + _a.institutionName + '"';
		        				e+= ' data-illegal_id="' + _a.id + '"';
		        				if ( _a.uploadEntity && _a.uploadEntity.reportImgId ){
		        					e+= ' data-report_img_id="' + _a.uploadEntity.reportImgId + '"';
		        				}
		        				if ( _a.appeal && _a.appeal.appealAtta ){ //申诉图片
        							e+= ' data-appeal_atta="' + _a.appeal.appealAtta + '"';
        						}
	        			e+='>';
		        			// 诊所
	        				e+='<td class="text-left">'+  _a.institutionName + '</td>';
	        				// 年月
	        				e+='<td class="text-center">'+ new Date(_a.checkDt).format('yyyy-MM'); + '</td>';
		        			// 状态
	        				e+='<td class="text-center">'+ (_a.checkSt==0?'正常':'<font style="color:red">异常</font>')+'</td>';
		        			// 状态说明
						    e+='<td class="text-center">'+ (_a.message !=null?_a.message:'-') + '</td>';
	        				if ( _a.uploadEntity && _a.uploadEntity.reportImgId ){ // 有报告数据时
	        					// 报告单
		        				e+='<td class="text-center"><button type="button" name="showImage" class="btn btn-primary btn-sm illegal-btn">查看</button><button type="button" name="download"style="margin-left: 5px;" class="btn btn-primary btn-sm illegal-btn">下载</button></td>';
		        				// 检测日期
		        				e+='<td class="text-center">'+ (new Date(_a.uploadEntity.checkDate).format('yyyy-MM-dd')) +'</td>';
	        				} else {
	        					e+='<td class="text-center">-</td>';
	        					e+='<td class="text-center">-</td>';
	        				}
	        				// 申诉状态
        					if ( _a.appeal ){
        						e+='<td class="text-center">'+ MY_APPEAL.getAppealStTxt(_a.appeal.appealSt) +'</td>';// 申述状态
        						e+='<td class="text-center">'+ (_a.appeal.appeal==null?'-':_a.appeal.appeal) +'</td>';// 申述内容
        						// 申述图片
        						if ( _a.appeal && _a.appeal.appealAtta ){
        							e+='<td class="text-center"><button type="button" name="showAppealAtta" class="btn btn-default btn-sm">查看</button></td>';
        						} else {
        							e+='<td class="text-center">-</td>';
        						}
        						e+='<td class="text-center">'+ (_a.appeal.feedback==null?'-':_a.appeal.feedback) +'</td>';//处理意见	
        						e+='<td class="text-center">'+ (_a.appeal.supervisorName==null?'-':_a.appeal.supervisorName) +'</td>';	// 处理人
        						// 操作按钮区  根据申诉表的信息控制显示的按钮
        						//  报告模块比较特殊：每日check定时钟会生成异常数据，诊所提交报告后，又会把异常数据订正成正常数据。
        						// 故此处有如下 case:有申诉表数据，但是状态已经被修改为正常为了。
        						if (_a.checkSt == -1){
        							if ( _a.appeal.appealSt == 1){
    									e+='<td class="text-center"><button type="button" name="check" class="btn btn-default btn-sm illegal-btn">申诉处理</button></td>';
    								} else if ( _a.appeal.appealSt == 0 ) {
    									e+='<td class="text-center"><button type="button" name="closeIllegal" class="btn btn-default btn-sm illegal-btn">取消异常</button></td>';
    								} else {
    									e+='<td class="text-center">-</td>';
    								}
        						} else {
        							e+='<td class="text-center">-</td>';
        						}
        					}else{
        						e+='<td class="text-center">-</td>';
        						e+='<td class="text-center">-</td>';
        						e+='<td class="text-center">-</td>';
        						e+='<td class="text-center">-</td>';
        						e+='<td class="text-center">-</td>';
        						e+='<td class="text-center">-</td>';
        					}
	        			e+='</tr>';
	        		});
	        		$tbody.append(e);
	        		
	        	} else if (data.errcode == -1 ) {
	        		WeKnow.errorMsg( data.errMsg );
	        	} else {
	        		WeKnow.systemMsg('查询失败，请重新登录后再试');
	        	}
	        }
		});
	}
	return {
		loadItem:loadItem
	}
})();